package com.flowable.controller.oa;

import com.common.core.controller.BaseController;
import com.common.core.domain.AjaxResult;
import com.common.core.domain.entity.SysUser;
import com.common.core.page.TableDataInfo;
import com.common.utils.SecurityUtils;
import com.common.utils.StringUtils;
import com.common.utils.bean.BeanUtils;
import com.flowable.controller.oa.vo.BpmOALeaveCreateReqVO;
import com.flowable.controller.oa.vo.BpmOALeavePageReqVO;
import com.flowable.controller.oa.vo.BpmOALeaveRespVO;
import com.flowable.service.IFlowTaskService;
import com.flowable.service.impl.BpmOALeaveService;
import com.system.domain.oa.BpmOALeaveDO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Tag(name = "管理后台 - OA 请假申请")
@RestController
@RequestMapping("/flowable/leave")
public class BpmOALeaveController extends BaseController {

    @Autowired
    private BpmOALeaveService leaveService;

    @Autowired
    private IFlowTaskService flowTaskService;

    @PostMapping("/create")
    public AjaxResult createLeave(@Valid @RequestBody BpmOALeaveCreateReqVO createReqVO){


      return    AjaxResult.success(leaveService.createLeave(createReqVO));
    }

//    @GetMapping("/get")
//    @Operation(summary = "获得请假申请")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    public AjaxResult getLeave(@RequestParam("id") Long id) {
//        BpmOALeaveDO leave = leaveService.getLeave(id);
//        return  AjaxResult.success(BeanUtils.toBean(leave, BpmOALeaveRespVO.class));
//    }

    @GetMapping("/page")
    @Operation(summary = "获得请假申请分页")
    public TableDataInfo list(BpmOALeavePageReqVO user)
    {
        startPage();
        List<BpmOALeaveDO> list = leaveService.selectList(user);
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        for (BpmOALeaveDO u:list) {
            if(StringUtils.isNotNull(u.getProcessInstanceId())){
                u.setIsAuditor(flowTaskService.IsAuditor(sysUser.getUserId().toString(),u.getProcessInstanceId()));
            }else {
                u.setIsAuditor(false);
            }
        }
        return getDataTable(list);
    }




}
